package top.atcra.rpc.utils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

public class BinaryPrinter {
    public static void printAsBinary(ByteBuf byteBuf) {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.getBytes(byteBuf.readerIndex(), bytes);
        String binaryString = ByteBufUtil.hexDump(bytes);
        StringBuilder formattedBinary = new StringBuilder();
        for (int i = 0; i < binaryString.length(); i += 2) {
            formattedBinary.append(binaryString, i, i + 2).append(" ");
        }
        StringBuilder index = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            index.append(String.format("%02d", i)).append(" ");
        }
        System.out.println("Index:" + index);
        System.out.println("bExpr:" + formattedBinary);
    }
}
